package com.xiaoKe.zhonghzu.global.tool;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.global.base.BaseApplication;
import com.xiaoKe.zhonghzu.ui.onClick.OnClickListenerPositionObject;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期时间工具类，提供日期格式化、比较、选择器等功能
 */
public class PvOptions {
    // 常用日期时间格式
    private static final String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
    private static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm";
    private static final String FORMAT_DATE = "yyyy-MM-dd";
    private static final String FORMAT_MONTH = "yyyy-MM";
    private static final String FORMAT_TIME = "HH:mm";

    /**
     * 颜色转换
     */
    public static int getColor(int colorResId) {
        return ContextCompat.getColor(BaseApplication.instance, colorResId);
    }



    /**
     * 日期选择器 - 精确到天
     */
    public static void showDatePicker(Context context, OnDateSelectedListener listener) {
        showTimePicker(context, new boolean[]{true, true, true, false, false, false},
                FORMAT_DATE, null, null, listener);
    }

    /**
     * 日期时间选择器 - 精确到分钟
     */
    public static void showDateTimePicker(Context context, OnDateSelectedListener listener) {
        showTimePicker(context, new boolean[]{true, true, true, true, true, false},
                FORMAT_DATE_TIME, null, null, listener);
    }

    /**
     * 月份选择器
     */
    public static void showMonthPicker(Context context, OnDateSelectedListener listener) {
        showTimePicker(context, new boolean[]{true, true, false, false, false, false},
                FORMAT_MONTH, null, null, listener);
    }

    /**
     * 时间选择器 - 精确到分钟
     */
    public static void showTimePicker(Context context, OnDateSelectedListener listener) {
        showTimePicker(context, new boolean[]{false, false, false, true, true, false},
                FORMAT_TIME, null, null, listener);
    }

    /**
     * 通用时间选择器
     */
    private static void showTimePicker(Context context, boolean[] type, String formatPattern,
                                       Calendar startDate, Calendar endDate,
                                       OnDateSelectedListener listener) {
        Calendar selectedDate = Calendar.getInstance();

        TimePickerView pvTime = new TimePickerBuilder(context, (date, v) -> {
            String formattedDate = new SimpleDateFormat(formatPattern, Locale.getDefault()).format(date);
            if (listener != null) {
                listener.onDateSelected(formattedDate);
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setContentTextSize(18)
                .setSubmitColor(getColor(R.color.colorMain))
                .setCancelColor(getColor(R.color.colorMain))
                .setType(type)
                .setLineSpacingMultiplier(1.2f)
                .setTextXOffset(0, 0, 0, 0, 0, 0)
                .isCenterLabel(false)
                .build();

        pvTime.show();
    }
    //初始化选择器方法具体参数参考github
    public static OptionsPickerView initOptionPicker(Context context, OnOptionsSelectListener onOptionsSelectListener) {
        return new OptionsPickerBuilder(context, onOptionsSelectListener)
                .setTitleText(context.getString(R.string.请选择))
                .setContentTextSize(16)
                .setTitleSize(12).setSubCalSize(12)
//                .setDividerColor(getView().getResources().getColor(R.color.color111d2a9))
                .setSelectOptions(0).isCenterLabel(true)
                .setBgColor(context.getResources().getColor(R.color.white)) //设置选择列表的背景颜色
                .setTitleBgColor(context.getResources().getColor(R.color.white))//标题的背景色
                .setTitleColor(context.getResources().getColor(R.color.color666))//标题的颜色
                .setCancelColor(context.getResources().getColor(R.color.colorMain))//取消的文字颜色
                .setSubmitColor(context.getResources().getColor(R.color.colorMain))//确定的文字颜色
                .isRestoreItem(true)
//                .setOutSideColor(getResources().getColor(R.color.xian)) //设置透明度
                .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
                    @Override
                    public void onOptionsSelectChanged(int options1, int options2, int options3) {
                        //滑动事件
                    }
                })
                .build();

    }
    /**
     * 日期选择回调接口
     */
    public interface OnDateSelectedListener {
        void onDateSelected(String date);
    }
}